# -*- coding: UTF-8 -*-
import os

from langchain_core.messages import SystemMessage, HumanMessage
from langchain_community.chat_models import ChatTongyi
from app.project.doc_to_recommendation.llm.model.base_model import BaseModel

os.environ["DASHSCOPE_API_KEY"] = "sk-31b7cded97ac43c7a581359d142f0a09"

class TongyiChatBaseModel(BaseModel):
    def __init__(self, config: dict):
        super().__init__(config)
        model = config.get('model', 'qwen-max')
        self.bind_t = config.get('bind_tools', False)
        temperature = config.get('temperature', 0.3)
        self.model = ChatTongyi(model=model)
        self.tools = []
        if self.bind_t:
            self.bind_tools_df()

    def bind_tools_df(self):
        pass

    def bind_tools(self, tools):
        self.tools = tools
        self.model = self.model.bind_tools(tools)

    def agent_calls(self, text, image, prompt=None):
        # 构造输入信息
        call_message = []
        if prompt:
            call_message.append(SystemMessage(content=prompt))
        human_mes_content = [
            {"type": "text", "text": text},
        ]
        if image:
            img_json_obj = {
                "type": "image_url",
                "image_url": {"url": image}
            }
            human_mes_content.append(img_json_obj)
        call_message.append(HumanMessage(content=human_mes_content))

        response = self.model.invoke(call_message)
        # 外呼llm
        return response